<?php

global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$redis_prefix = $this->model('model')->get_prefix();
		$currentyear = date('Y');
		$currentmonth = date('m');
		$years = array();
		$i = 0;
		while ($i <= 10) {
			$years[] = $currentyear + $i;
			++$i;
		}
		$months = array('01','02','03','04','05','06','07','08','09','10','11','12');
		$return['currentyear']=$currentyear;
		$return['currentmonth']=$currentmonth;
		$return['months']=$months;
		$return['years']=$years;	
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if(!$year){
			$year = 	$currentyear;
		}
		if(!$month){
			$month = 	$currentmonth;
		}
		$day = date("t",strtotime($year.'-'.$month));
		$calendar = $this->model('model')->redis()->hGetAll($redis_prefix . 'calendar_' . $year . '_' . $month);
		if (empty($calendar)) {
			$calendar = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$calendar[date($year . '-' . $month . '-' . $i)] = false;
				++$i;
			}
		}else{
			$result = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$result[$date] = false;
				if (isset($calendar[$date])) {
					$value = trim($calendar[$date]);
					$result[$date] = false;
					if (!(empty($value))) {
						 $title=pdo_getcolumn('jy_yushou_task', array('id'=>$value),'title');
						$result[$date] = array('taskid' => $value, 'title' =>$title);
					}
				}
				++$i;
			}
			$calendar = $result;
		}
		$d=array();
		foreach($calendar as $k=>$v){
			$d[$k]=date('d', strtotime($k));
		}
		$return['calendar'] = $calendar;		
		$return['week'] = date('w', strtotime(date($year . '-' . $month . '-1')));
		$return['kongday']=array();
		for($i=0;$i<$return['week']-1;$i++){
			$return['kongday'][]=$i;
		}
		$return['d'] = $d;
		exi($return);
}
if($_RQ['op']=='dates'){
		$redis_prefix = $this->model('model')->get_prefix();
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		$day = date("t",strtotime($year.'-'.$month));
		$calendar = $this->model('model')->redis()->hGetAll($redis_prefix . 'calendar_' . $year . '_' . $month);
		if (empty($calendar)) {
			$calendar = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$calendar[date($year . '-' . $month . '-' . $i)] = false;
				++$i;
			}
		}else {
			$result = array();
			$i = 1;
			while ($i <= $day) {
				if ($i < 10) {
					$i = '0' . $i;
				}
				$date = $year . '-' . $month . '-' . $i;
				$result[$date] = false;
				if (isset($calendar[$date])) {
					$value = trim($calendar[$date]);
					$result[$date] = false;
					if (!(empty($value))) {
						 $title=pdo_getcolumn('jy_yushou_task', array('id'=>$value),'title');
						$result[$date] = array('taskid' => $value, 'title' =>$title);
					}
				}
				++$i;
			}
			$calendar = $result;
		}
		$d=array();
		foreach($calendar as $k=>$v){
			$d[$k]=date('d', strtotime($k));
		}
		$return['calendar'] = $calendar;
		$return['week'] = date('w', strtotime(date($year . '-' . $month . '-1')));
		$return['year']=$year;
		$return['month']=$month;
		$return['kongday']=array();
		for($i=0;$i<$return['week']-1;$i++){
			$return['kongday'][]=$i;
		}
		exi($return);
}
if($_RQ['op']=='set'){
	$postdata=get_postdata();
	//print_r($postdata);exit();
		$taskid = $postdata['taskid'];
		$date = $postdata['date'];
		if (empty($taskid) || empty($date)) {
			exi('参数错误','error');
		}
		$redis_prefix = $this->model('model')->get_prefix();
		$time = strtotime($date);
		$year = date('Y', $time);
		$month = date('m', $time);
		$task = pdo_get('jy_yushou_task',array('id'=>$taskid),array('id','title'));
		if (empty($task)) {
			exi('任务未找到','error');
		}
		$this->model('model')->redis()->hSet($redis_prefix . 'calendar_' . $year . '_' . $month, date('Y-m-d', $time), $taskid);
		exi('设置成功','','/calendar/');
		//exi(array('taskid' => $task['id'], 'title' => $task['title']));
}
if($_RQ['op']=='delete'){
		$postdata=get_postdata();
		$date = trim($postdata['date']);
		if (empty($date)) {
			exi('参数错误，请选择日期！'.$date,'error');
		}
		$time = strtotime($date);
		$year = date('Y', $time);
		$month = date('m', $time);
		$redis_prefix = $this->model('model')->get_prefix();
		$this->model('model')->redis()->hDel($redis_prefix . 'calendar_' . $year . '_' . $month, $date);
		exi('删除成功') ;
}
if($_RQ['op']=='clear'){
		$postdata=get_postdata();
		$year = trim($postdata['year']);
		$month = trim($postdata['month']);
		$redis_prefix = $this->model('model')->get_prefix();
		$this->model('model')->redis()->delete($redis_prefix . 'calendar_' . $year . '_' . $month);
		exi('清除成功') ;
}
if($_RQ['op']=='batch_set'){
		$taskid = intval($_RQ['taskid']);
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if ($month < 10) {
			$month = '0' . $month;
		}
		$days = $_RQ['days'];
		if (empty($taskid) || empty($year) || empty($month)) {
			exi('参数错误','error');
		}
		if (!(is_array($days)) || empty($days)) {
			exi('参数错误','error');
		}
		//$task = pdo_fetch('select id ,title from ' . tablename('ewei_shop_seckill_task') . ' where uniacid=:uniacid and id=:id limit 1', array(':uniacid' => $_AR['uniacid'], ':id' => $taskid));
		$task = pdo_get('jy_yushou_task',array('id'=>$taskid),array('id','title'));
		if (empty($task)) {
			exi('任务未找到','error');
			
		}
		if ($days[0] == 'all') {
			array_shift($days);
		}
		$maxday = 31;
		$arr = array();
		$dates = array();
		$i = 1;
		while ($i <= $maxday) {
			if ($i < 10) {
				$i = '0' . $i;
			}
			$date = date($year . '-' . $month . '-' . $i);
			$week = date('w', strtotime($date));
			if ($week == 0) {
				$week = 7;
			}
			if (in_array($week, $days)) {
				$arr[$date] = $taskid;
				$dates[] = $date;
			}
			++$i;
		}
		$redis_prefix = $this->model('model')->get_prefix();
		$this->model('model')->redis()->hMset($redis_prefix . 'calendar_' . $year . '_' . $month, $arr);
		exi(array('taskid' => $task['id'], 'title' => $task['title'], 'dates' => implode(',', $dates)));
	}
if($_RQ['op']=='batch_delete'){
		$year = trim($_RQ['year']);
		$month = trim($_RQ['month']);
		if ($month < 10) {
			$month = '0' . $month;
		}
		$days = $_RQ['days'];
		if (empty($year) || empty($month)) {
			exi('参数错误','error');
		}
		if (!(is_array($days)) || empty($days)) {
			exi('参数错误','error');
		}
		if ($days[0] == 'all') {
			array_shift($days);
		}
		$redis = $this->model('model')->redis();
		$redis_prefix = $this->model('model')->get_prefix();
		$calendar = $redis->hGetAll($redis_prefix . 'calendar_' . $year . '_' . $month);

		if (!(is_array($calendar))) {
			$calendar = array();
		}
		$maxday = get_last_day($year, $month);
		$dates = array();
		$i = 1;
		while ($i <= $maxday) {
			if ($i < 10) {
				$i = '0' . $i;
			}
			$date = date($year . '-' . $month . '-' . $i);
			$week = date('w', strtotime($date));

			if ($week == 0) {
				$week = 7;
			}
			if (in_array($week, $days)) {
				if (is_array($calendar) && isset($calendar[$date])) {
					unset($calendar[$date]);
					$redis->hDel($redis_prefix . 'calendar_' . $year . '_' . $month, $date);
					$dates[] = $date;
				}

			}
			++$i;
		}

		if (empty($calendar)) {
			$redis->delete($redis_prefix . 'calendar_' . $year . '_' . $month);
		}
		exi(array('dates' => implode(',', $dates)));
	}
?>
